<?php
App::uses('Sanitize','Utility');
class ProductsController extends AppController {
	var $uses = array('Product','Brand','Category','Size','Stock');
	//var $components = array('Image');
    public function index() {
		$conditions = array();
		$name = $category_id = $brand_id = $product_code = '';		 
		if($this->request->is('get')){						
			if(!empty($this->params->query['name'])){
				$name = Sanitize::escape($this->params->query['name']);
				$conditions['and']['name like'] = "%$name%";
			}
			if(!empty($this->params->query['product_code'])){
				$product_code = Sanitize::escape($this->params->query['product_code']);
				$conditions['and']['product_code like'] = "%$product_code%";
			}
			if(!empty($this->params->query['category_id'])){
				$category_id = Sanitize::escape($this->params->query['category_id']);
				$conditions['and']['category_id'] = $category_id;
			}
			if(!empty($this->params->query['brand_id'])){
				$brand_id = Sanitize::escape($this->params->query['brand_id']);
				$conditions['and']['brand_id'] = $brand_id;
			}
		}
		$this->paginate = array(
			'fields'=>array('Product.id','Product.product_code','Product.name','Product.del_flag','Product.photo','Product.photo_dir','Category.name','Brand.name'),
			'limit'=>10,
			'recursive'=>0,
			'conditions'=>$conditions,
			'order'=>'Product.id desc'
		);
        $rs = $this->paginate('Product');
		$opt_cat = $this->Category->find('list',array('fields'=>array('id','name')));
		$opt_brand = $this->Brand->find('list',array('fields'=>array('id','name')));
		$this->set(compact('rs','name','brand_id','category_id','product_code','opt_cat','opt_brand'));
		if($this->request->is('ajax')){
			$this->layout = 'ajax';
			$this->render('ajax_search');
		}
    }
	
	public function add() {
        if (!empty($this->data)) {
			foreach($this->data['Stock'] as $key=>$item){
				if (empty($item['amount'])) unset($this->request->data['Stock'][$key]);
			}
            $this->Product->create();			
			$this->Product->set($this->data);
			$this->Stock->set($this->data);
			if($this->Product->validates() && $this->Stock->validates()){
				if ($this->Product->saveAssociated($this->data)) {
					//Save upload image
					//upload_image($fileData,$size,$subFolder,$prefix) - tam thoi xai thu plugin upload
					//$this->Image->upload_image($this->data['Product']['bigsize'],'400x600');
					$this->Session->setFlash(__('Đã được lưu'));
					$this->redirect(array('action' => '.'));
				} else {
					$this->Session->setFlash(__('Có lỗi, không lưu được, xin thử lại'));
				}
			}
        }
		$sizes = $this->Size->find('all',array('fields'=>array('id','name')));
		$opt_cat = $this->Category->find('list',array('fields'=>array('id','name')));
		$opt_brand = $this->Brand->find('list',array('fields'=>array('id','name')));		
		$this->set(compact('opt_cat','opt_brand','sizes'));
    }

    public function edit($id = null) {
		$this->Product->unbindModel(array(
			'belongsTo' => array('Category','Brand'),
		));
		$this->Product->recursive = 1;
        $this->Product->id = $id;
        if (!$this->Product->exists()) {
            throw new NotFoundException(__('Không tìm thấy'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
			foreach($this->data['Stock'] as $key=>$item){
				if (empty($item['amount'])) unset($this->request->data['Stock'][$key]);
			}
			$this->Product->set($this->data);
			$this->Stock->set($this->data);
			if($this->Product->validates() && $this->Stock->validates()){
				$this->Stock->deleteAll(array('product_id'=>$this->Product->id));
				if ($this->Product->saveAssociated($this->data)) {
					$this->Session->setFlash(__('Đã được lưu'));
					$this->redirect(array('action' => '.'));
				} else {
					$this->Session->setFlash(__('Có lỗi, không lưu được, xin thử lại'));
				}
			}
        } else {			
			$data = $this->Product->read(null, $id);
			$tmp = array();
			foreach($data['Stock'] as $key=>$item){
				$tmp[$item['size_id']-1] = $item;
			}
			$data['Stock'] = $tmp;
            $this->request->data = $data;
        }
		$sizes = $this->Size->find('all',array('fields'=>array('id','name')));
		$opt_cat = $this->Category->find('list',array('fields'=>array('id','name')));
		$opt_brand = $this->Brand->find('list',array('fields'=>array('id','name')));
		$this->set(compact('opt_cat','opt_brand','sizes'));
    }

    public function delete($id = null) {
		if($id==null) die("No ID received");		
		if($this->Product->delete($id)==false){
			$this->Session->setFlash('Bị lỗi không xóa được');
		}
		else {
			$this->Session->setFlash('Item đã được xóa khỏi danh sách');
		}
		$this->redirect(array('action'=>'index'));
    }
}
